package com.dh.project.controller;

import com.dh.project.model.*;
import com.dh.project.other.ModelRes;
import com.dh.project.service.*;
import com.dh.project.view.OrderDetailView;
import com.dh.project.view.OrderListView;
import com.dh.project.view.OrderServiceProjectView;
import com.dh.project.view.ServiceProjectListView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.*;

/**
 * Created by admin on 2017/4/25.
 */
@Controller
public class OrderController {


    private OrderInfoService orderInfoService;

    private CustomerAddressService customerAddressService;

    private OrderServiceProjectService orderServiceProjectService;

    private ServiceProjectService serviceProjectService;

    private CouponService couponService;

    private CustomerCouponService customerCouponService;

    @Autowired
    public OrderController(OrderInfoService orderInfoService,CustomerAddressService customerAddressService,
                           OrderServiceProjectService orderServiceProjectService,ServiceProjectService serviceProjectService,
                           CouponService couponService,CustomerCouponService customerCouponService){
        this.orderInfoService = orderInfoService;
        this.customerAddressService = customerAddressService;
        this.orderServiceProjectService = orderServiceProjectService;
        this.serviceProjectService = serviceProjectService;
        this.couponService = couponService;
        this.customerCouponService = customerCouponService;
    }

    /**
     * 查询工人订单列表
     * @param employee_id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "selectEmployeeOrderList",produces = "text/html;charset=utf-8")
    public Object selectEmployeeOrderList(int employee_id){
        Map map = new HashMap();
        map.put("employee_id",employee_id);
        List<OrderListView> orderListViews = orderInfoService.selectOrderListByMap(map);
        return new ModelRes(orderListViews);
    }

    /**
     * 查询订单
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "selectEmployeeOrderDetail",produces = "text/html;charset=utf-8")
    public Object selectEmployeeOrderDetail(int order_id){
        Map map = new HashMap();
        map.put("order_id",order_id);
        OrderDetailView orderDetailView = orderInfoService.selectOrderDetailByMap(map);
        List<OrderServiceProjectView> orderServiceProjects = orderServiceProjectService.selectServiceProjectList(map);

        List<ServiceProjectListView> serviceProjectListViews = new ArrayList<ServiceProjectListView>();
        List<ServiceProjectListView> maintainList = new ArrayList<ServiceProjectListView>();
        for (int i = 0; i < orderServiceProjects.size(); i++) {
            ServiceProjectListView serviceProjectListView = new ServiceProjectListView();
            serviceProjectListView.setOrder_service_id(orderServiceProjects.get(i).getId());
            serviceProjectListView.setService_name(orderServiceProjects.get(i).getService_name());
            serviceProjectListView.setService_price(orderServiceProjects.get(i).getService_price().doubleValue());
            serviceProjectListView.setService_id(orderServiceProjects.get(i).getService_id());
            serviceProjectListView.setIndoor_price(orderServiceProjects.get(i).getIndoor_price());
            serviceProjectListView.setOrder_price(orderServiceProjects.get(i).getOrder_price());
            if (orderServiceProjects.get(i).getService_class() != 7){
                serviceProjectListViews.add(serviceProjectListView);
            }else {
                maintainList.add(serviceProjectListView);
            }
        }
        orderDetailView.setServiceProjectListViews(serviceProjectListViews);
        orderDetailView.setOverhaulList(maintainList);
        return new ModelRes(ModelRes.Status.SUCCESS,"查询成功",orderDetailView);
    }

//    /**
//     * 订单添加服务项
//     * @param order_id
//     * @param service_id
//     * @return
//     */
//    @ResponseBody
//    @RequestMapping(value = "orderAddServiceProject",produces = "text/html;charset=utf-8")
//    public Object orderAddServiceProject(int order_id ,int service_id){
//        ServiceProject serviceProject = serviceProjectService.selectById(Long.valueOf(service_id));
//        OrderServiceProject orderServiceProject = new OrderServiceProject();
//        orderServiceProject.setOrder_id(order_id);
//        orderServiceProject.setService_id(service_id);
//        orderServiceProject.setService_name(serviceProject.getService_name());
//        orderServiceProject.setService_price(serviceProject.getService_all_price().doubleValue());
//        orderServiceProject.setCreatetime(String.valueOf(new Date().getTime()));
//        orderServiceProject.setModifytime(String.valueOf(new Date().getTime()));
//        boolean isSuccess = orderServiceProjectService.insert(orderServiceProject);
//        if (isSuccess){
//            return new ModelRes();
//        }else {
//            return new ModelRes(ModelRes.Status.ERROR,"添加服务项失败");
//        }
//    }

//    /**
//     * 修改订单状态
//     * @param order_id
//     * @param order_status
//     * @return
//     */
//    @ResponseBody
//    @RequestMapping(value = "updateOrderStatus",produces = "text/html;charset=utf-8")
//    public Object updateOrderStatus(int order_id,int order_status) {
//        OrderInfo orderInfo = orderInfoService.selectById(Long.valueOf(order_id));
//        orderInfo.setOrder_status(order_status);
//        boolean isSuccess = orderInfoService.updateById(orderInfo);
//        if (isSuccess){
//            return new ModelRes(ModelRes.Status.SUCCESS,"修改成功",orderInfo);
//        }else {
//            return new ModelRes(ModelRes.Status.ERROR,"修改失败");
//        }
//    }

    /**
     * 工人添加服务项
     * order_id
     * stringServiceId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "orderAddServiceProject",produces = "text/html;charset=utf-8")
    public Object orderAddServiceProject(int order_id,String stringServiceId) {

        String[] ids = stringServiceId.split(",");
        double allPrice = 0.0;
        List<ServiceProjectListView> serviceProjectListViews = new ArrayList<ServiceProjectListView>();
        for (int i = 0; i < ids.length; i++) {
            ServiceProject serviceProject = serviceProjectService.selectById(Integer.valueOf(ids[i]));
            OrderServiceProject orderServiceProject = new OrderServiceProject();
            orderServiceProject.setOrder_id(order_id);
            orderServiceProject.setService_id(serviceProject.getService_id());
            orderServiceProject.setService_name(serviceProject.getService_name());
            orderServiceProject.setService_price(serviceProject.getService_all_price().doubleValue());
            orderServiceProject.setCreatetime(String.valueOf(new Date().getTime()));
            orderServiceProject.setModifytime(String.valueOf(new Date().getTime()));
            allPrice = allPrice + serviceProject.getService_all_price().doubleValue();
            boolean isSuccess = orderServiceProjectService.insert(orderServiceProject);
            if (!isSuccess){
                return new ModelRes(ModelRes.Status.ERROR,"后台错误，添加服务项失败！");
            }

            ServiceProjectListView serviceProjectListView = new ServiceProjectListView();
            serviceProjectListView.setOrder_service_id(orderServiceProject.getId());
            serviceProjectListView.setService_name(serviceProject.getService_name());
            serviceProjectListView.setService_price(serviceProject.getService_all_price().doubleValue());
            serviceProjectListViews.add(serviceProjectListView);
        }


        OrderInfo orderInfo = orderInfoService.selectById(Long.valueOf(order_id));
        orderInfo.setOrder_status(4);
        orderInfo.setOrder_price(BigDecimal.valueOf(orderInfo.getOrder_price().doubleValue()+allPrice));
        boolean isSuccess = orderInfoService.updateById(orderInfo);
        if (isSuccess){
            return new ModelRes(serviceProjectListViews);
        }else {
            return new ModelRes(ModelRes.Status.ERROR,"添加失败");
        }
    }


    /**
     * 确认服务项和检修费
     * @param order_id
     * @param all_service_id
     * @param maintain_id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "orderAddAllProject",produces = "text/html;charset=utf-8")
    public Object orderAddAllProject(int order_id,String all_service_id,String maintain_id) {

        OrderInfo oldOrderInfo = orderInfoService.selectById(order_id);

        if (oldOrderInfo.getOrder_status() == 3 || oldOrderInfo.getOrder_status() == 4 || oldOrderInfo.getOrder_status() == 5) {
            Map deleteOrderMap = new HashMap();
            deleteOrderMap.put("order_id", order_id);
            List<OrderServiceProject> orderServiceProjects = orderServiceProjectService.selectByMap(deleteOrderMap);

            double oldAllPrice = 0.0;
            for (int i = 0; i < orderServiceProjects.size(); i++) {
                oldAllPrice = oldAllPrice + orderServiceProjects.get(i).getService_price();
                orderServiceProjectService.deleteById(orderServiceProjects.get(i).getId());
            }
            oldOrderInfo.setOrder_price(BigDecimal.valueOf(oldOrderInfo.getOrder_price().doubleValue() - oldAllPrice));
            orderInfoService.updateById(oldOrderInfo);

            List<ServiceProjectListView> serviceProjectListViews = new ArrayList<ServiceProjectListView>();
            //总金额
            double allPrice = 0.0;
            //添加产品
            if (!all_service_id.equals("-1")) {
                String[] ids = all_service_id.split(",");
                for (int i = 0; i < ids.length; i++) {
                    ServiceProject serviceProject = serviceProjectService.selectById(Integer.valueOf(ids[i]));
                    OrderServiceProject orderServiceProject = new OrderServiceProject();
                    orderServiceProject.setOrder_id(order_id);
                    orderServiceProject.setService_id(serviceProject.getService_id());
                    orderServiceProject.setService_name(serviceProject.getService_name());
                    orderServiceProject.setService_price(serviceProject.getService_all_price().doubleValue());
                    orderServiceProject.setCreatetime(String.valueOf(new Date().getTime()));
                    orderServiceProject.setModifytime(String.valueOf(new Date().getTime()));
                    allPrice = allPrice + serviceProject.getService_all_price().doubleValue();
                    boolean isSuccess = orderServiceProjectService.insert(orderServiceProject);
                    if (!isSuccess) {
                        return new ModelRes(ModelRes.Status.ERROR, "后台错误，添加服务项失败！");
                    }
                    ServiceProjectListView serviceProjectListView = new ServiceProjectListView();
                    serviceProjectListView.setService_id(orderServiceProject.getService_id());
                    serviceProjectListView.setOrder_service_id(orderServiceProject.getId());
                    serviceProjectListView.setService_name(serviceProject.getService_name());
                    serviceProjectListView.setService_price(serviceProject.getService_all_price().doubleValue());
                    serviceProjectListViews.add(serviceProjectListView);
                }
            }

            //添加维修费
            if (!maintain_id.equals("-1")) {

                Integer int_maintain_id = Integer.valueOf(maintain_id);
                OrderInfo orderInfo = orderInfoService.selectById(order_id);
                Map map = new HashMap();
                map.put("order_id", order_id);
                OrderServiceProject orderServiceProject1 = orderServiceProjectService.selectByOrderId(map);
                ServiceProject serviceProject = serviceProjectService.selectById(int_maintain_id);
                int id = 0;
                if (null == orderServiceProject1) {
                    OrderServiceProject orderServiceProject = new OrderServiceProject();
                    orderServiceProject.setOrder_id(order_id);
                    orderServiceProject.setService_id(serviceProject.getService_id());
                    orderServiceProject.setService_name(serviceProject.getService_name());
                    orderServiceProject.setService_price(serviceProject.getService_all_price().doubleValue());
                    orderServiceProject.setCreatetime(String.valueOf(new Date().getTime()));
                    orderServiceProject.setModifytime(String.valueOf(new Date().getTime()));
                    boolean isSuccess = orderServiceProjectService.insert(orderServiceProject);
                    if (!isSuccess) {
                        return new ModelRes(ModelRes.Status.ERROR, "后台错误，添加服务项失败！");
                    }
                    id = orderServiceProject.getId();
                    orderInfo.setOrder_price(BigDecimal.valueOf(orderInfo.getOrder_price().doubleValue() + serviceProject.getService_all_price().doubleValue()));
                    orderInfoService.updateById(orderInfo);
                } else {
                    double price = orderInfo.getOrder_price().doubleValue() - orderServiceProject1.getService_price().doubleValue() + serviceProject.getService_all_price().doubleValue();
                    orderServiceProject1.setService_id(serviceProject.getService_id());
                    orderServiceProject1.setService_name(serviceProject.getService_name());
                    orderServiceProject1.setService_price(serviceProject.getService_all_price().doubleValue());
                    orderServiceProject1.setModifytime(String.valueOf(new Date().getTime()));
                    orderInfo.setOrder_price(BigDecimal.valueOf(price));
                    orderInfoService.updateById(orderInfo);
                    orderServiceProjectService.updateById(orderServiceProject1);
                    id = orderServiceProject1.getId();
                }
                ServiceProjectListView serviceProjectListView = new ServiceProjectListView();
                serviceProjectListView.setOrder_service_id(id);
                serviceProjectListView.setService_id(serviceProject.getService_id());
                serviceProjectListView.setService_name(serviceProject.getService_name());
                serviceProjectListView.setService_price(serviceProject.getService_all_price().doubleValue());
                serviceProjectListViews.add(serviceProjectListView);
            }
            OrderInfo orderInfo = orderInfoService.selectById(Long.valueOf(order_id));
            orderInfo.setOrder_status(4);
            orderInfo.setOrder_price(BigDecimal.valueOf(orderInfo.getOrder_price().doubleValue() + allPrice));
            boolean isSuccess = orderInfoService.updateById(orderInfo);
            if (isSuccess) {
                return new ModelRes(serviceProjectListViews);
            } else {
                return new ModelRes(ModelRes.Status.ERROR, "添加列表失败!");
            }
        }else {
            return new ModelRes(ModelRes.Status.ERROR, "该状态下不允许添加服务!");
        }
    }

    /**
     * 订单添加检修费
     * @param order_id
     * @param maintain_id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "orderAddMaintain",produces = "text/html;charset=utf-8")
    public Object orderAddServiceProject(int order_id,int maintain_id) {
        OrderInfo orderInfo = orderInfoService.selectById(order_id);
        Map map = new HashMap();
        map.put("order_id",order_id);
        OrderServiceProject orderServiceProject1 = orderServiceProjectService.selectByOrderId(map);
        ServiceProject serviceProject = serviceProjectService.selectById(maintain_id);
        int id = 0;
        if (null == orderServiceProject1){
            OrderServiceProject orderServiceProject = new OrderServiceProject();
            orderServiceProject.setOrder_id(order_id);
            orderServiceProject.setService_id(serviceProject.getService_id());
            orderServiceProject.setService_name(serviceProject.getService_name());
            orderServiceProject.setService_price(serviceProject.getService_all_price().doubleValue());
            orderServiceProject.setCreatetime(String.valueOf(new Date().getTime()));
            orderServiceProject.setModifytime(String.valueOf(new Date().getTime()));
            boolean isSuccess = orderServiceProjectService.insert(orderServiceProject);
            if (!isSuccess){
                return new ModelRes(ModelRes.Status.ERROR,"后台错误，添加服务项失败！");
            }
            id = orderServiceProject.getId();
            orderInfo.setOrder_price(BigDecimal.valueOf(orderInfo.getOrder_price().doubleValue() + serviceProject.getService_all_price().doubleValue()));
            orderInfoService.updateById(orderInfo);
        }else {
            double price = orderInfo.getOrder_price().doubleValue() - orderServiceProject1.getService_price().doubleValue() + serviceProject.getService_all_price().doubleValue();
            orderServiceProject1.setService_id(serviceProject.getService_id());
            orderServiceProject1.setService_name(serviceProject.getService_name());
            orderServiceProject1.setService_price(serviceProject.getService_all_price().doubleValue());
            orderServiceProject1.setModifytime(String.valueOf(new Date().getTime()));
            orderInfo.setOrder_price(BigDecimal.valueOf(price));
            orderInfoService.updateById(orderInfo);
            orderServiceProjectService.updateById(orderServiceProject1);
            id = orderServiceProject1.getId();
        }

        ServiceProjectListView serviceProjectListView = new ServiceProjectListView();
        serviceProjectListView.setOrder_service_id(id);
        serviceProjectListView.setService_name(serviceProject.getService_name());
        serviceProjectListView.setService_price(serviceProject.getService_all_price().doubleValue());
        return new ModelRes(ModelRes.Status.SUCCESS,"添加成功",serviceProjectListView);
    }

    /**
     * 工人删除订单服务项
     * @param service_id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "deleteOrderServiceProject",produces = "text/html;charset=utf-8")
    public Object deleteOrderServiceProject(int order_id,int service_id) {
        OrderInfo orderInfo = orderInfoService.selectById(order_id);
        OrderServiceProject orderServiceProject = orderServiceProjectService.selectById(service_id);
        orderInfo.setOrder_price(BigDecimal.valueOf(orderInfo.getOrder_price().doubleValue() - orderServiceProject.getService_price()));
        if (orderInfo.getOrder_status() != 3 && orderInfo.getOrder_status() != 5){
            return new ModelRes(ModelRes.Status.ERROR,"现订单状态不能删除服务项！");
        }
        orderInfo.setOrder_status(4);
        orderInfoService.updateById(orderInfo);
        boolean isSuccess = orderServiceProjectService.deleteById(service_id);
        if (isSuccess){
            return new ModelRes();
        }else {
            return new ModelRes(ModelRes.Status.ERROR,"删除失败");
        }
    }

    /**
     * 工人清除订单服务项
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "deleteAllOrderServiceProject",produces = "text/html;charset=utf-8")
    public Object deleteOrderServiceProject(int order_id) {
        OrderInfo orderInfo = orderInfoService.selectById(order_id);

        Double AllPrice = 0.0;
        Map map = new HashMap();
        map.put("order_id",order_id);

        List<OrderServiceProject> orderServiceProjectList = orderServiceProjectService.selectByMap(map);
        for (int i = 0; i < orderServiceProjectList.size(); i++) {
            AllPrice = AllPrice + orderServiceProjectList.get(i).getService_price();
            boolean isSuccess = orderServiceProjectService.deleteById(orderServiceProjectList.get(i).getId());
            if (!isSuccess){
                return new ModelRes(ModelRes.Status.ERROR,"删除步骤出错");
            }
        }
        orderInfo.setOrder_price(BigDecimal.valueOf(orderInfo.getOrder_price().doubleValue() - AllPrice));
        orderInfo.setOrder_status(4);
        orderInfoService.updateById(orderInfo);

        return new ModelRes();
    }

    /**
     * 工人确认收款
     * @param order_id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "employeeAffirmPay",produces = "text/html;charset=utf-8")
    public Object employeeAffirmPay(int order_id) {
        OrderInfo orderInfo = orderInfoService.selectById(order_id);
        if(orderInfo.getOrder_status() != 6){
            return new ModelRes(ModelRes.Status.ERROR,"该订单状态下不能确认收款！");
        }
        orderInfo.setModifytime(String.valueOf(new Date().getTime()));
        orderInfo.setOrder_status(7);
        orderInfoService.updateById(orderInfo);
        return new ModelRes();
    }

}
